<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';

      run(async function () {
        const {
          React: { createElement },
          ReactDOM: { render },
          WebChat: { ReactWebChat }
        } = window; // Imports in UMD fashion.

        const ponyfill = lolex.createClock();
        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill });

        const App = () =>
          createElement(ReactWebChat, {
            directLine,
            ponyfill,
            store,
            styleOptions: {
              typingAnimationBackgroundImage: `url('')`
            }
          });

        render(createElement(App), document.getElementById('webchat'));

        await pageConditions.webChatRendered();

        // Wait for "Connecting..." message to dismiss
        ponyfill.tick(600);

        await pageConditions.uiConnected();

        // ---

        // WHEN: A contentless message is received.
        await directLine.emulateIncomingActivity({
          channelData: { streamSequence: 1, streamType: 'streaming' },
          from: { id: 'u-00001', name: 'Bot', role: 'bot' },
          id: 't-00001',
          type: 'typing'
        });

        // THEN: Should render the typing indicator.
        await waitFor(() => expect(pageElements.typingIndicator).toBeTruthy());
        await host.snapshot('local');

        // ---

        // WHEN: After 60 seconds.
        ponyfill.tick(60_000);

        // THEN: Should continue to render the typing indicator.
        await waitFor(() => expect(pageElements.typingIndicator).toBeTruthy());
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
